/**
 * 
 * MooModernizr 1.0
 * 
 * @author Arian Stolwijk
 * @license MIT
 * 
 */

// These are the original notes of the original author

/*!
 * Modernizr JavaScript library 1.0c
 * http://modernizr.com/
 *
 * Copyright (c) 2009 Faruk Ates - http://farukat.es/
 * Licensed under the MIT license.
 * http://modernizr.com/license/
 *
 * Featuring major contributions by
 * Paul Irish  - http://paulirish.com
 * Ben Alman   - http://benalman.com/
 */
/*
 * Modernizr is a script that will detect native CSS3 and HTML5 features
 * available in the current UA and provide an object containing all
 * features with a true/false value, depending on whether the UA has
 * native support for it or not.
 * 
 * In addition to that, Modernizr will add classes to the <body>
 * element of the page, one for each cutting-edge feature. If the UA
 * supports it, a class like "cssgradients" will be added. If not,
 * the class name will be "no-cssgradients". This allows for simple
 * if-conditionals in CSS styling, making it easily to have fine
 * control over the look and feel of your website.
 * 
 * @author		Faruk Ates
 * @copyright	 (2009) Faruk Ates.
 *
 * @contributor   Paul Irish
 * @contributor   Ben Alman
 */


(function(){
	
	var m = new Element('mooModernizr'),
		f = new Element('input'); // input types check
	
	// Private funtion
	// Loop through all possible properties and see if we get a valid response at all
	checkProps = function(prop){
		var i = 0;
		for (i in prop) {
			if (getCss(prop[i]) !== null) {
				return prop[i];
			}
		}
		return false;		
	};
	
	/**
	 * 
	 * @return {Bool|String} The javascript style index (e.g. mozBackgroundImage)
	 * @param {String} prop style index (e.g. padding)
	 * @param {String|Array} combine other style index(es)
	 * @param {Bool} noPrefix if the origional style index should not be included
	 */
	checkPropsAll = function(prop,combine,noPrefix){
		var uc_prop = prop.replace( /./, function(a) {
			return a.toUpperCase(); 
		}),
		props = [
			'webkit' + uc_prop,
			'Moz' + uc_prop,
			'moz' + uc_prop,
			'o' + uc_prop,
			'ms' + uc_prop
		];
		if(noPrefix !== true){
			props.include(prop);
		}
		if (combine) {
			props.combine($type(combine) == 'array' ? combine : [combine]);
		}
		return checkProps(props);
	};
	
	setCss = function(str){
		m.style.cssText = str;
	};
	
	setCssAll = function(str1,str2){
		str1 += ';';

		return setCss(
			str1
			+ '-webkit-' + str1
			+ '-moz-' + str1
			+ '-o-' + str1
			+ '-ms-' + str1
			+ ( str2 || '' )
		);
	};
	
	getCss = function(prop){
		prop = prop ? prop : 'cssText';
		return $defined(m.style[prop]) ? m.style[prop] : null;
	}


	// Public functions
	this.mooModernizr = {
		
		/**
		 * @return boolean
		 * @param {String} type
		 */
		inputtype: function(type){
			f.setAttribute('type',type);
			return !!(f.type !== 'text');
		}
			
	};
	
	// The features hash holds all the tests
	var features = new Hash({
		
		/**
		 * @return {Bool}
		 */
		canvas: (function(){
			return !!new Element('canvas').getContext;
		})()
		
	});
	
	// The features hash is extended so canvastext can access features.canvas		
	features.extend({

		/**
		 * @return {Bool}
		 */
		canvastext: (function(){
			!!(features.canvas && typeof new Element('canvas').getContext('2d').fillText == 'function')
		})(),
		
		/**
		 * geolocation tests for the new Geolocation API specification.
		 *   This test is a standards compliant-only test; for more complete
		 *   testing, including a Google Gears fallback, please see
		 *   
		 *   http://code.google.com/p/geo-location-javascript/
		 * 
		 * @return {Bool}
		 */
		geolocation: !!navigator.geolocation,
		
		/**
		 * @return {Bool}
		 */
		rgba: (function(){
			// Set an rgba() color and check the returned value
			setCss('background-color: rgba(150,255,150, .5)');
			return getCss('backgroundColor').contains('rgba');
		})(),
		
		/**
		 * @return {Bool}
		 */
		hsla: (function(){
			// Same as rgba(), in fact, browsers re-map hsla() to rgba() internally
			setCss('background-color: hsla(120,40%,100%, .5)');
			return getCss('backgroundColor').contains('hsla');
		})(),
		
		/**
		 * @return {Bool}
		 */
		multiplebgs: (function(){
			// Setting multiple images and a color on the background shorthand property
			//  and then querying the style.background property value for the number of
			//  occurrences of "url(" is a reliable method for detecting ACTUAL support for this!
			setCss('background: url(m.png), url(a.png), #f99 url(m.png)');
		
			// If the UA supports multiple backgrounds, there should be three occurrences
			//	of the string "url(" in the return value for elem_style.background
			
			return /(url\s*\(.*?){3}/.test(getCss('background'));
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		borderimage: (function(){
			setCssAll('border-image:url(m.png) 1 1 stretch');
			return checkPropsAll('borderImage');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		borderradius: (function(){
			setCssAll('border-radius: 10px');
			return checkPropsAll('borderRadius');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		boxshadow: (function(){
			setCssAll('box-shadow: #000 1px 1px 3px');
			return checkPropsAll('boxShadow');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		textshadow: (function(){
			setCssAll('text-shadow: 1px 1px 3px #000');
			return checkPropsAll('textShadow');
		})(),
		
		/**
		 * @return {Bool}
		 */
		opacity: (function(){
			// Browsers that actually have CSS Opacity implemented have done so
			//  according to spec, which means their return values are within the
			//  range of [0.0,1.0] - including the leading zero.
			setCss('opacity: .5');
			return getCss('opacity').contains('0.5');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		cssanimations: (function(){
			setCssAll('animation:"animate" 2s ease 2', 'position:relative');
			return checkPropsAll('animationName');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		csscolumns: (function(){
			setCssAll('column-count: 3');
			return checkPropsAll('columnCount');
		})(),
		
		/**
		 * @return {Bool}
		 */
		cssgradients: (function(){
			/**
			 * For CSS Gradients syntax, please see:
			 * http://webkit.org/blog/175/introducing-css-gradients/
			 */

			var str1 = 'background-image:',
			str2 = 'gradient(linear,left top,right bottom,from(#9f9),to(white));';

			setCss(str1 + str2
				+ str1 + '-webkit-' + str2
				+ str1 + '-moz-' + str2
				+ str1 + '-o-' + str2
				+ str1 + '-ms-' + str2
			);
			return getCss('backgroundImage').contains('gradient');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		cssreflections: (function(){
			setCssAll('box-reflect: right 1px');
			return checkPropsAll('boxReflect');
		})(),
		
		/**
		 * @return {Bool|String}
		 */
		csstransforms: (function(){
			setCssAll('transform:rotate(3deg)');
			return checkPropsAll('transform','transformProperty',true);
		})(),

		/**
		 * @return {Bool|String}
		 */
		csstransforms3d: (function(){
			setCssAll('perspective: 500');
			return checkPropsAll('perspective','perspectiveProperty',true);
		})(),

		/**
		 * @return {Bool|String}
		 */
		csstransitions: (function(){
			setCssAll('transition: all .5s linear');
			return checkPropsAll('transitionProperty');
		})(),
		
		/**
		 * @return {Bool|Object} If video is supported, also check the playtypes ogg/mp4
		 */
		video: (function(){
			var v = new Element('video');
			var video = new Hash({supported: !!(v.canPlayType)});
			if(video.supported){
				video.extend({
					ogg: (function(){
						var r =  v.canPlayType('video/ogg; codecs="theora, vorbis"');
						return r == "" ? false : r;
					})(),
					mp4: (function(){
						var r = v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
						return r == "" ? false : r;
					})()
				});
				return video;
			}
			return false
		})(),
		
		/**
		 * @return {Bool|Object}
		 */
		audio: (function(){
			var a = new Element('audio');
			var audio = new Hash({supported: !!(a.canPlayType)});
			if(audio.supported){
				audio.extend({
					ogg: (function(){
						var r =  a.canPlayType('audio/ogg; codecs="vorbis"');
						return r == "" ? false : r;
					})(),
					wave: (function(){
						var r = a.canPlayType('audio/wave');
						return r == "" ? false : r;
					})()
				});
				return audio;
			}
			return false
		})(),
		
		/**
		 * @return {Bool}
		 */		
		query: !!(document.querySelector),
		
		/**
		 * @return {Bool}
		 */		
		json: !!(window.JSON),
		
		inputtypes: (function(props) {
			var inputs = {}, supported = false;
			props.each(function(type){
				inputs[type] = mooModernizr.inputtype(type);
				supported = supported || inputs[type];
			});
			return supported ? inputs : false;
		})('search tel url email datetime date month week time datetime-local number range color'.split(' ')),

		/**
		 * @return {Bool}
		 */		
		inputplaceholder: (function(){
			return 'placeholder' in f;
		})(),

		/**
		 * @return {Bool}
		 */		
		inputautofocus: (function(){
			return 'autofocus' in f;
		})(),

		/**
		 * @return {Bool}
		 */		
		localstorage: !!(window.localStorage),
		
		/**
		 * @return {Bool}
		 */		
		webworkers: !!(window.Worker),
		
		/**
		 * @return {Bool}
		 */		
		applicationcache: !!(window.applicationCache),
		
		/**
		 * @version isFontFaceSupported - v0.7 - 9/24/2009
		 * 
		 * @author Copyright (c) 2009 Paul Irish
		 * @license MIT license
		 * http://paulirish.com/2009/font-face-feature-detection/
		 * @return {Bool}
		 */		 
		fontface: (function(){
			
			var fontret,
				fontfaceCheckDelay = 100;
		 
				// IE supports EOT and has had EOT support since IE 5.
				// This is a proprietary standard (ATOW) and thus this off-spec,
				// proprietary test for it is acceptable. 
			if (!(!/*@cc_on@if(@_jscript_version>=5)!@end@*/0)) fontret = true;
		 
			else {
		 
			// Create variables for dedicated @font-face test
			  var doc = document, docElement = doc.documentElement, 
				  st  = doc.createElement('style'),
				  spn = doc.createElement('span'),
				  wid, nwid, isFakeBody = false, body = doc.body,
				  callback, isCallbackCalled;
		 
			  // The following is a font-face  glyph definition for the . character:
			  st.textContent = "@font-face{font-family:testfont;src:url('data:font/ttf;base64,AAEAAAAMAIAAAwBAT1MvMliohmwAAADMAAAAVmNtYXCp5qrBAAABJAAAANhjdnQgACICiAAAAfwAAAAEZ2FzcP//AAMAAAIAAAAACGdseWYv5OZoAAACCAAAANxoZWFk69bnvwAAAuQAAAA2aGhlYQUJAt8AAAMcAAAAJGhtdHgGDgC4AAADQAAAABRsb2NhAIQAwgAAA1QAAAAMbWF4cABVANgAAANgAAAAIG5hbWUgXduAAAADgAAABPVwb3N03NkzmgAACHgAAAA4AAECBAEsAAUAAAKZAswAAACPApkCzAAAAesAMwEJAAACAAMDAAAAAAAAgAACbwAAAAoAAAAAAAAAAFBmRWQAAAAgqS8DM/8zAFwDMwDNAAAABQAAAAAAAAAAAAMAAAADAAAAHAABAAAAAABGAAMAAQAAAK4ABAAqAAAABgAEAAEAAgAuqQD//wAAAC6pAP///9ZXAwAAAAAAAAACAAAABgBoAAAAAAAvAAEAAAAAAAAAAAAAAAAAAAABAAIAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEACoAAAAGAAQA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BnAGgAdABoAHQAdABwADoALwAvAHMAYwByAGkAcAB0AHMALgBzAGkAbAAuAG8AcgBnAC8ATwBGAEwAAAAAAgAAAAAAAP+BADMAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAQACAQIAEQt6ZXJva2F5YWhsaQ==')}";
			  doc.getElementsByTagName('head')[0].appendChild(st);
		 
		 
			  spn.setAttribute('style','font:99px _,serif;position:absolute;visibility:hidden'); 
		 
			  if  (!body){
				body = docElement.appendChild(doc.createElement('fontface'));
				isFakeBody = true;
			  } 
		 
			  // the data-uri'd font only has the . glyph; which is 3 pixels wide.
			  spn.innerHTML = '........';
			  spn.id		= 'fonttest';
		 
			  body.appendChild(spn);
			  wid = spn.offsetWidth;
			  spn.style.font = '99px testfont,_,serif';
		 
			  // needed for the CSSFontFaceRule false positives (ff3, chrome, op9)
			  fontret = wid !== spn.offsetWidth;
		 
			  var delayedCheck = function(){
				fontret = wid !== spn.offsetWidth;
		 
				callback && (isCallbackCalled = true) && callback(fontret);
				isFakeBody && setTimeout(function(){body.parentNode.removeChild(body)},50);
			  }
		 
			  setTimeout(delayedCheck,fontfaceCheckDelay);
			}
		 
			function ret(){ return fontret || wid !== spn.offsetWidth; };
		 
			// allow for a callback
			ret.ready = function(fn){
			  (isCallbackCalled || fontret) ? fn(fontret) : (callback = fn);
			}  
		 
			return ret();
		})()
		
	});
		
	// Extend the browser features object
	$extend(Browser.Features,features);

	// Clean css
	setCss('');

})();


